package com.heimo.agentlicence.task;

import java.util.List;

import org.apache.http.NameValuePair;

import android.content.Context;
import android.util.Log;

import com.heimo.agentlicence.htmlchecker.DisciplineResultChecker;

public class DisciplineCheckTask extends BasicTask {
	private final String TAG = "DisciplineCheckTask";
	public DisciplineCheckTask(Context ctx, OnTaskCompleted completeAction) {
		super(ctx);
		this.listener = completeAction;
		this.loadMsg = "Loading licence information...";
	}

	public void fnLoadPage(List<NameValuePair> nvps) {
		try {
			super.onPreExecute();
			Log.d(TAG, "DisciplineCheckTask.fnLoadPage()");
			super.execute(LOAD_DISCIPLINE_CHECK_PAGE,nvps);
		} catch (Exception e) {
			this.listener.onTaskFailed(e.getMessage());
		}
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		Log.d(TAG, "DisciplineCheckTask.onPostExecute()");
		DisciplineResultChecker lc = new DisciplineResultChecker();
		boolean hasRecord = false;
		try {
			hasRecord = lc.fnGetRecordFromHtml(result);
			if (this.listener != null) {
				this.listener.onTaskCompleted(hasRecord);
			}
		} catch (Exception e) {
			this.listener.onTaskFailed(e.getMessage());
		}
		
	}

	public void setCompletedAction(OnTaskCompleted completeAction) {
		this.listener = completeAction;
	}

}